﻿using Corina.Datamodel.DataObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Corina.DataContext.Master;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using Corina.Common;

namespace Corina.Webservice.Cydee
{
    /// <summary>
    /// Summary description for Device
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Device : WebserviceBase
    {
        #region Device
        [WebMethod]
        public ResultInfo GetDeviceItems(int kind, string deviceSn, int saleFlag, int index, int size)
        {
            return GetInstance<DeviceMaster>().GetFetchedItems(deviceSn, index, size, saleFlag, kind);
        }

        [WebMethod]
        public ResultInfo InsertDevice(string account, string password, int kind, string deviceSn, DateTime? manufactureDate, double ratePrice, int dataModule)
        {
            DeviceInfo entity = new DeviceInfo()
            {
                DeviceKindId = kind,
                DeviceSn = deviceSn,
                DataTransferTypeId = dataModule,
                ManufactureDate = manufactureDate,
                RatePrice = ratePrice
            };

            return GetInstance<DeviceMaster>().Update(account, password, entity, EntityOperation.Insert);
        }

        [WebMethod]
        public ResultInfo RemoveDevice(string account, string password, int id)
        {
            DeviceInfo entity = new DeviceInfo()
            {
                Id = id
            };

            return GetInstance<DeviceMaster>().Update(account, password, entity, EntityOperation.Delete);
        }
        #endregion

        #region Device kind
        [WebMethod]
        public ResultInfo GetDeviceKindItems()
        {
            return GetInstance<DeviceKindMaster>().GetAllItems();
        }

        [WebMethod]
        public ResultInfo InsertDeviceKind(string account, string password, string name, string description)
        {
            DeviceKindInfo entity = new DeviceKindInfo()
            {
                Name = name,
                Description = description
            };

            return GetInstance<DeviceKindMaster>().Update(account, password, entity, EntityOperation.Insert);

        }

        [WebMethod]
        public ResultInfo UpdateDeviceKind(string account, string password, string name, string description, int id)
        {
            DeviceKindInfo entity = new DeviceKindInfo()
            {
                Name = name,
                Description = description,
                Id = id
            };

            return GetInstance<DeviceKindMaster>().Update(account, password, entity, EntityOperation.Update);

        }

        [WebMethod]
        public ResultInfo RemoveDeviceKind(string account, string password, int id)
        {
            DeviceKindInfo entity = new DeviceKindInfo()
            {
                Id = id
            };

            return GetInstance<DeviceKindMaster>().Update(account, password, entity, EntityOperation.Delete);

        }
        #endregion

        #region DeviceDataTransferType
        [WebMethod]
        public ResultInfo GetAllDeviceDataTransferTypeItems(string account, string password)
        {
            return GetInstance<DeviceDataTransferTypeMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo InsertDeviceDataTransferType(string account, string password, string title, string description, bool avavailability)
        {
            ResultInfo result = new ResultInfo(ResultValue.Unknown);

            bool exist = GetInstance<MasterBase>().ExistsEntity<DeviceDataTransferTypeInfo>(DatabaseKeys.CORINA_PROD, new List<string>() { "Title" }, new List<object>() { title });

            if (exist)
            {
                result.Result = ResultValue.Exists;
            }
            else
            {

                DeviceDataTransferTypeInfo entity = new DeviceDataTransferTypeInfo()
                {
                    Title = title,
                    Description = description,
                    IsAvailable = avavailability
                };

                result = GetInstance<DeviceDataTransferTypeMaster>().Update(account, password, entity, EntityOperation.Insert);
            }

            return result;
        }

        [WebMethod]
        public ResultInfo UpdateDeviceDataTransferType(string account, string password, string title, string description, bool avavailability, int id)
        {
            DeviceDataTransferTypeInfo entity = new DeviceDataTransferTypeInfo()
            {
                Id = id,
                Title = title,
                Description = description,
                IsAvailable = avavailability
            };

            return GetInstance<DeviceDataTransferTypeMaster>().Update(account, password, entity, EntityOperation.Update);
        }

        [WebMethod]
        public ResultInfo RemoveDeviceDataTransferType(string account, string password, int id)
        {
            return GetInstance<DeviceDataTransferTypeMaster>().Update(account, password, new DeviceDataTransferTypeInfo() { Id = id }, EntityOperation.Delete);
        }

        [WebMethod]
        public ResultInfo ChangeDeviceDataTransferTypeAvavailability(string account, string password, int id, bool availability)
        {
            return GetInstance<DeviceDataTransferTypeMaster>().ChangeAvailability(account, password, id, availability);
        }
        #endregion

        #region Device sale
        [WebMethod]
        public ResultInfo GetAllDeviceSaleItems(string account, string password, string deviceSn, int kindId, int userId, int index, int size)
        {
            return GetInstance<DeviceSaleMaster>().GetAllItems(account, password, deviceSn, kindId, userId, index, size);
        }

        [WebMethod]
        public ResultInfo InsertDeviceSale(string account, string password, string deviceSn, int userId, DateTime saleDate, double price, string salesperson)
        {
            DeviceSaleInfo entity = new DeviceSaleInfo()
            {
                DeviceSn = deviceSn,
                SaleDate = saleDate,
                Salesperson = salesperson,
                Price = price,
                UserId = userId
            };

            return GetInstance<DeviceSaleMaster>().Insert(account, password, entity);
        }

        [WebMethod]
        public ResultInfo InsertDeviceSaleExt(string account, string password, string jsonString)
        {
            List<DeviceSaleInfo> entities = JsonHelper.Parse<List<DeviceSaleInfo>>(jsonString);
            return GetInstance<DeviceSaleMaster>().Insert(account, password, entities);
        }
        #endregion

        #region Specimen Kind
        [WebMethod]
        public ResultInfo GetSpecimenKindItems()
        {
            return GetInstance<SpecimenKindMaster>().GetAllItems();
        }

        [WebMethod]
        public ResultInfo InsertSpecimenKind(string account, string password, string title)
        {

            return GetInstance<SpecimenKindMaster>().Insert(account, password, title);

        }

        [WebMethod]
        public ResultInfo UpdateSpecimenKind(string account, string password, string title, int id)
        {

            return GetInstance<SpecimenKindMaster>().Update(account, password, id, title);

        }

        [WebMethod]
        public ResultInfo RemoveSpecimenKind(string account, string password, int id)
        {
            return GetInstance<SpecimenKindMaster>().Remove(account, password, id);

        }
        #endregion

        #region Auto Resolve
        [WebMethod]
        public ResultInfo GetAutoResolveItems(string account, string password, int deviceKindId, string deviceSn, int index, int size)
        {
            return GetInstance<AutoResolveMaster>().GetAllItems(account, password, deviceKindId, deviceSn, index, size);
        }

        [WebMethod]
        public ResultInfo UpdateAutoResolve(string account, string password, int id, bool autoresolve)
        {
            return GetInstance<AutoResolveMaster>().Update(account, password, id, autoresolve);
        }

        [WebMethod]
        public ResultInfo InsertAutoResolve(string account, string password, string deviceSn)
        {
            return GetInstance<AutoResolveMaster>().Insert(account, password, deviceSn);
        }

        [WebMethod]
        public ResultInfo RemoveAutoResolve(string account, string password, int id)
        {
            return GetInstance<AutoResolveMaster>().Remove(account, password, id);
        }
        #endregion
    }
}
